package com.qfedu.buy.server.service.impl;

import com.qfedu.buy.common.config.RabbitMQConstantConfig;
import com.qfedu.buy.common.config.SystemConfig;
import com.qfedu.buy.common.dto.UserScoreDto;
import com.qfedu.buy.common.util.DateUtil;
import com.qfedu.buy.common.util.TokenUtil;
import com.qfedu.buy.server.entity.Usersign;
import com.qfedu.buy.server.dao.UsersignDao;
import com.qfedu.buy.server.service.intf.UsersignService;
import com.qfedu.buy.server.vo.R;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.jws.Oneway;
import java.util.Random;

/**
 * @program: NeighborBuy
 * @description: 邻里购真正意义上最懂你的新零售平台
 * @author: Feri(老邢)
 * @create: 2021-01-26 14:22:27
 */
@Service
public class UsersignServiceImpl implements UsersignService{
    /*
    *注入持久层对象*/
    @Autowired
    private UsersignDao dao;

    @Autowired
    private RabbitTemplate rabbitTemplate;
    /*
    * 新增内容*/
    @Override
    public R save(Usersign usersign) {
        if(usersign!=null){
            if(dao.insert(usersign)>0){
                return R.ok();
            }
        }
        return R.fail("新增异常");
    }
    /*
    * 查询 所有数据*/
    @Override
    public R queryAll() {
        return R.ok(dao.selectAll());
    }

    @Override
    public R checkSign(String token) {
        int uid=TokenUtil.getUid(token);
        if(uid>0){
            Usersign usersign=dao.selectLastSign(uid);
            if(usersign==null){
                //没有过签到
                return R.ok();
            }else{
                //校验签到的日期是不是今天
                if(DateUtil.eqdate(usersign.getCtime())){
                    //今日已经签过道
                    return R.fail("亲，今日已签到");
                }else{
                    return R.ok();
                }
            }
        }else {
            return R.fail("请，登陆已经失效");
        }
    }

    @Override
    public R sign(String token) {
        //签到
        int uid=TokenUtil.getUid(token);
        if(uid>0){
            Usersign usersign=dao.selectLastSign(uid);
            Usersign sign=new Usersign();
            Random random=new Random();
            int score=random.nextInt(SystemConfig.USER_AWARD_SIGN)+1;
            if(usersign==null){
                //第一次签到
                sign.setScore(score);
                sign.setExtrascore(SystemConfig.USER_AWARD_SIGNFIRST);
                sign.setDays(1);
            }
            else{
                //校验签到的日期是不是今天
                if(DateUtil.eqdate(usersign.getCtime())){
                    //今日已经签过道
                    return R.fail("亲，今日已签到");
                }else{
                    //今日未签到
                    //验证是否为连续签到
                    if(DateUtil.eqyesterday(usersign.getCtime())){
                        //连续签到
                        sign.setDays(usersign.getDays()+1);
                        if(sign.getDays()%365==0){
                            //连续签到日期为整年
                            sign.setExtrascore(SystemConfig.USER_AWARD_SIGN365);
                        }else if(sign.getDays()%30==0){
                            sign.setExtrascore(SystemConfig.USER_AWARD_SIGN30);
                        }else if(sign.getDays()%7==0){
                            sign.setExtrascore(score*SystemConfig.USER_AWARD_SIGN7);
                        }else if(sign.getDays()%3==0){
                            sign.setExtrascore(SystemConfig.USER_AWARD_SIGN3);
                        }
                        sign.setScore(score);
                    }else{
                        //断签
                        sign.setScore(score);
                    }
                }
            }
            //签到数据的新增
            dao.insert(sign);
            //发送通知 RabbitMQ 积分新增
            UserScoreDto scoreDto=new UserScoreDto();
            scoreDto.setUid(uid);
            scoreDto.setScore(sign.getScore()+sign.getExtrascore());
            rabbitTemplate.convertAndSend("", RabbitMQConstantConfig.QUEUE_USER_LEVEL_SOCRE, scoreDto);
            return R.ok();
        }
        return R.fail();
    }
}